/* eslint-disable array-callback-return */
import { message } from 'antd';
import {
  getInvRecordList,
  getInvRuleList,
  getContList,
  getInvRecordDetail,
  getInvInfoList,
  getInvRuleDetail,
  getInvRuleCostAll,
  getInvApplyDataBillOnceFee,
  getInvApplyCompany,
  cancelReasonSubmit,
  exchangeInvSubmit,
  refuseInvSubmit,
  getInvRuleCostDetail,
} from '@/services/invoice';

export default {
  state: {
    formFieldsValue: {},
    activeKey: '1',
    tabList: [
      { itemCode: '1', itemName: '发票申请记录', isHas: false },
      { itemCode: '2', itemName: '开票规则设置', isHas: false },
    ],
    expandForm: false,
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    invRecordDetail: {}, // 发票申请记录详情数据
    invCompany: {}, // 换票回显数据
    invInfoList: {}, // 发票信息数据
    invRuleDetail: {}, // 发票规则详情数据
    invRuleCostAll: [], // 发票规则成本中心表格数据
    invRuleCostDetail: {}, // 发票规则成本中心详情数据
    valuesArr: [
      'custId',
      'custSuppId',
      'invState',
      'contId',
      'applyTime',
      'invRuleType',
    ]
  },
  effects: {
    // 获取发票申请记录列表
    *fetchInvRecordList({ payload }, { call, put }) {
      const response = yield call(getInvRecordList, payload);
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '获取发票记录列表错误'}`);
      }
    },
    // 获取发票规则列表
    *fetchInvRuleList({ payload }, { call, put }) {
      const response = yield call(getInvRuleList, payload);
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '获取发票规则列表错误'}`);
      }
    },
    // 获取客户，供应商合同模糊查询
    *fetchContList({ payload }, { call, put }) {
      const response = yield call(getContList, payload);
      yield put({
        type: 'updateModalData',
        payload: {
          contList: [],
        },
      });
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'updateModalData',
            payload: {
              contList: response.data,
            },
          });
        } else {
          message.warning('获取合同数据：服务端错误');
        }
      } else {
        message.warning(response.message || '获取合同数据：服务端错误');
      }
    },
    // 获取发票记录详情数据
    *getInvRecordDetail({ payload }, { call, put }) {
      const response = yield call(getInvRecordDetail, payload);
      yield put({
        type: 'updateModalData',
        payload: {
          invRecordDetail: {},
          invInfoList: {},
        },
      });
      if (response.success) {
        const invTableData = yield call(getInvInfoList, payload);
        if (invTableData.success) {
          if (response.data) {
            if (
              response.data.invBill &&
              response.data.invBill.sumAmount &&
              invTableData.data.sumAmount
            ) {
              invTableData.data.noSumAmount =
                response.data.invBill.sumAmount - invTableData.data.sumAmount;
            }
            yield put({
              type: 'updateModalData',
              payload: {
                invRecordDetail: response.data,
                invInfoList: invTableData.data,
              },
            });
          } else {
            message.warning('获取发票申请记录数据：服务端错误');
          }
        } else {
          message.warning(response.message || '获取发票信息数据：服务端错误');
        }
      } else {
        message.warning(response.message || '获取发票申请记录数据：服务端错误');
      }
    },
    // 获取发票规则详情数据
    *getInvRuleDetail({ payload }, { call, put }) {
      const response = yield call(getInvRuleDetail, payload);
      yield put({
        type: 'updateModalData',
        payload: {
          invRuleDetail: {},
        },
      });
      if (response.success) {
        if (response.data) {
          // 处理发票规则 发票信息中的 邮寄地址
          if (response.data.invRulePanyTermMailDto && response.data.invRulePanyTermMailDto.length) {
            response.data.invRulePanyTermMailDto.map(item => {
              const temp = item;
              if (item.invMail && item.invMail.areaList && item.invMail.areaList.length) {
                temp.invMail.address = '';
                item.invMail.areaList.map((citys, index) => {
                  temp.invMail.address += (index > 0 ? '/' : '') + citys.areaName;
                });
                temp.invMail.address = `${item.invMail.address.trim()}/${item.invMail.mailAddr}`;
              }
            });
          }
          yield put({
            type: 'updateModalData',
            payload: {
              invRuleDetail: response.data,
            },
          });
        } else {
          message.warning('获取发票规则数据：服务端错误');
        }
      } else {
        message.warning(response.message || '获取发票规则数据：服务端错误');
      }
    },
    // 获取发票规则 成本中心发票信息
    *getInvRuleCostAll({ payload }, { call, put }) {
      const response = yield call(getInvRuleCostAll, payload);
      if (response.success) {
        yield put({
          type: 'updateModalData',
          payload: {
            invRuleCostAll: response.data || [],
          },
        });
      } else {
        message.warning(`${response.message || '获取成本中心发票信息错误：服务器端错误'}`);
      }
    },
    // 获取账单明细数据
    *getInvApplyDataBillOnceFee({ payload }, { call, put }) {
      const response = yield call(getInvApplyDataBillOnceFee, payload);
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '获取数据列表错误'}`);
      }
    },
    // 换票回显数据
    *getInvApplyCompany({ payload }, { call, put }) {
      const response = yield call(getInvApplyCompany, payload);
      yield put({
        type: 'updateModalData',
        payload: {
          invCompany: {},
        },
      });
      if (response.success) {
        if (response.data && response.data.length) {
          // 处理换票回显数据
          let invCompanyObj = null;
          if (response.data[0].invCompany) {
            invCompanyObj = response.data[0].invCompany;
          }
          yield put({
            type: 'updateModalData',
            payload: {
              invCompany: invCompanyObj,
            },
          });
        } else {
          message.warning('获取换票数据：服务端错误');
        }
      } else {
        message.warning(response.message || '获取换票数据：服务端错误');
      }
    },
    // 取消申请 取消原因的提交
    *cancelReasonSubmit({ payload }, { call }) {
      const response = yield call(cancelReasonSubmit, payload);
      return response;
    },
    // 换票提交数据
    *exchangeInvSubmit({ payload }, { call }) {
      const response = yield call(exchangeInvSubmit, payload);
      return response;
    },
    // 退票提交数据
    *refuseInvSubmit({ payload }, { call }) {
      const response = yield call(refuseInvSubmit, payload);
      return response;
    },
    // 获取发票规则成本中心详情
    *getInvRuleCostDetail({ payload }, { call, put }) {
      const response = yield call(getInvRuleCostDetail, payload);
      yield put({
        type: 'updateModalData',
        payload: {
          invRuleCostDetail: {},
        },
      });
      if (response.success) {
        if (response.data) {
          if (response.data.invRulePanyTermMailDto && response.data.invRulePanyTermMailDto.length) {
            response.data.invRulePanyTermMailDto.map(item => {
              if (item.invMailList && item.invMailList.length) {
                response.data.isHaveInvMail = '1';
                // 处理邮寄地址
                item.invMailList.map(citys => {
                  const temp = citys;
                  if (citys.areaList && citys.areaList.length) {
                    temp.address = '';
                    citys.areaList.map((values, index) => {
                      temp.address += (index > 0 ? '/' : '') + values.areaName;
                    });
                    temp.address = `${citys.address.trim()}/${citys.mailAddr}`;
                  }
                });
              } else {
                response.data.isHaveInvMail = '2';
              }
              return response.data;
            });
          }
          yield put({
            type: 'updateModalData',
            payload: {
              invRuleCostDetail: response.data,
            },
          });
        } else {
          message.warning('发票规则成本中心详情数据错误：服务端错误');
        }
      } else {
        message.warning(`${response.message || '发票规则成本中心详情数据错误'}`);
      }
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },

    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
